import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../components/Home.vue'
import UserVisit from '../components/UserVisit.vue'
import register from '../views/register.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Login
    },

    {
      path: '/Home',
      name: 'Home',
      component: Home
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'register',
      component:register
    },
    {
      path: '/UserVisit',
      name: 'UserVisit',
      component:UserVisit,
      children: [
        {
          path: '/UserVisit/AboutUs',
          name: 'AboutUs',
          component: () => import('../components/AboutUs.vue')
        },
          {
          path: '/UserVisit/UserAgent',
          name: 'UserAgent',
          component: () => import('../components/UserAgent.vue')
        },
          {
          path: '/UserVisit/PersonalPrivacyPolicy',
          name: 'PersonalPrivacyPolicy',
          component: () => import('../components/PersonalPrivacyPolicy.vue')
        },
          {
            path: '/UserVisit/ContactUs',
            name: 'ContactUs',
            component: () => import('../components/ContactUs.vue')
          },
      ]
    }

  ]
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token")
  if (token || to.path == '/login'|| to.path == '/UserVisit' ||to.path == '/UserVisit/AboutUs'
  || to.path == '/UserVisit/UserAgent' || to.path == '/UserVisit/PersonalPrivacyPolicy'
  || to.path == '/UserVisit/ContactUs')
  {
    next()
  } else {
    next('/login')
  }
})

export default router
